<template>
  <div class="talk">
    <button @click="talkStore.getLoveTalk()">获取一句土味情话</button>
    <ul>
      <li v-for="talk in talkList" :key="talk.id">{{ talk.title }}</li>
    </ul>
  </div>
</template>

<script setup lang="ts" name="LoveTalk">
  import axios from 'axios'
  import { nanoid } from 'nanoid'
  import { storeToRefs } from 'pinia'
  import { useTalkStore } from "@/store/talk"
  const talkStore = useTalkStore()
  const { talkList } = storeToRefs(talkStore)

  
  //方法不可以提取
  // const { getLoveTalk } = storeToRefs(talkStore)


  // console.log('@@@', talkStore);

  //第一种请求写法：
  // async function getLoveTalk() {
  //   // 发请求，下面这行的写法是：连续解构赋值+重命名
  //   let { data: { data } } = await axios.get('https://v2.xxapi.cn/api/weibohot')
  //   const title = data[Math.floor(Math.random() * data.length)]?.title
  //   //把请求回来的字符串，包装成一个对象
  //   let obj = { id: nanoid(), title }
  //   //放到数组中
  //   talkStore.talkList.unshift(obj)
  // }

  //第二种写法：
  function getLoveTalk() {
    talkStore.getLoveTalk()
  }

</script>

<style scoped>
  .talk {
    background-color: orange;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 0 0 10px;
  }
</style>
